package com.smart.parking.service.impl;


import com.smart.parking.entity.SpaceEntity;
import com.smart.parking.entity.VehicleEntity;
import com.smart.parking.service.SpaceService;
import com.smart.parking.service.SystemLogService;
import com.smart.parking.service.VehicleService;
import com.smart.parking.utils.PageUtils;
import com.smart.parking.utils.Query;
import com.smart.parking.vo.ParkingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.smart.parking.mapper.SystemLogMapper;
import com.smart.parking.entity.SystemLogEntity;


@Service("systemLogService")
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLogEntity> implements SystemLogService {

    @Autowired
    private SpaceService spaceService;

    @Autowired
    private VehicleService vehicleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SystemLogEntity> page = this.page(
                new Query<SystemLogEntity>().getPage(params),
                new QueryWrapper<SystemLogEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获得车位使用情况 获得不空闲车位列表
     */
    @Override
    public List<ParkingVO> getParkingListON() {
        List<ParkingVO> parkingVOList = new ArrayList<>();
        List<VehicleEntity> vehicleEntityList = vehicleService.list(new QueryWrapper<VehicleEntity>().eq("deleted", 0));
        vehicleEntityList.forEach(vehicleEntity -> {
            ParkingVO parkingVO = new ParkingVO();
            SpaceEntity spaceEntity = spaceService.getById(vehicleEntity.getSpaceId());
            this.copyParking(spaceEntity, parkingVO);
            parkingVO.setVehiclePlate(vehicleEntity.getVehiclePlate());
            parkingVOList.add(parkingVO);
        });
        return parkingVOList;
    }

    /**
     * 获得空闲车位列表
     */
    @Override
    public List<ParkingVO> getParkingListOFF() {
        List<ParkingVO> parkingVOList = new ArrayList<>();
        List<SpaceEntity> spaceEntityList = spaceService.list(new QueryWrapper<SpaceEntity>().eq("space_free", 0));
        spaceEntityList.forEach(spaceEntity -> {
            ParkingVO parkingVO = new ParkingVO();
            this.copyParking(spaceEntity, parkingVO);
            parkingVOList.add(parkingVO);
        });
        return parkingVOList;
    }

    public void copyParking(SpaceEntity spaceEntity, ParkingVO parkingVO) {
        parkingVO.setSpaceId(spaceEntity.getSpaceId());
        parkingVO.setSpaceFree(spaceEntity.getSpaceFree());
        parkingVO.setSpaceDamage(spaceEntity.getSpaceDamage());
    }

}
